// Constructs an income panel for couples with DV report 
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"


use "W:\intimate\data\match_victim_data_clean_new", clear


// Analysis 
g av_prior_emp = (ptoim1B3 + ptoim1B2 + ptoim1B1)/3
g av_prior_inc = (tyotuB3 + tyotuB2 + tyotuB1)/3


// merge with mother and father educ 
gen shnro_sec=sphnro0

merge m:1 shnro_sec using "$data_folder_final\fir_sec_gen_id_link_mother.dta", keepusing (mom_educ)
drop if _merge==2 
drop _merge 

merge m:1 shnro_sec using "$data_folder_final\fir_sec_gen_id_link_father.dta", keepusing (dad_educ)
drop if _merge==2 
drop _merge 

merge m:1 shnro_sec using "$data_folder_final\mother_education.dta", keepusing (mother_employed)
drop if _merge==2 
drop _merge 


rename mom_educ sp_mom_educ
rename dad_educ sp_dad_educ
rename mother_employed sp_mother_employed

replace sp_mom_educ=. if dv_couple==0 
replace sp_dad_educ=. if dv_couple==0 



bys match_id: gegen sp_mom_educ=max(sp_mom_educ), replace 
bys match_id: gegen sp_dad_educ=max(sp_dad_educ), replace 

gen sp_parent_EducGap=sp_dad_educ-sp_mom_educ 
gen sp_mom_college=(sp_mom_educ>=6 & sp_mom_educ!=.) 

// victim parents 
replace shnro_sec=shnro

merge m:1 shnro_sec using "$data_folder_final\fir_sec_gen_id_link_mother.dta", keepusing (mom_educ)
drop if _merge==2 
drop _merge 

merge m:1 shnro_sec using "$data_folder_final\fir_sec_gen_id_link_father.dta", keepusing (dad_educ)
drop if _merge==2 
drop _merge 

merge m:1 shnro_sec using "$data_folder_final\mother_education.dta", keepusing (mother_employed)
drop if _merge==2 
drop _merge 

replace mom_educ=. if dv_couple==0 
replace dad_educ=. if dv_couple==0 

bys match_id: gegen mom_educ=max(mom_educ), replace 
bys match_id: gegen dad_educ=max(dad_educ), replace 

gen parent_EducGap=dad_educ-mom_educ 
gen mom_college=(mom_educ>=6 & mom_educ!=.)




local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n



reshape long `varlist', i(group_id) j(time)

*Define dummy used in the eventstudies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"

lab var treatPost "Abusive x Cohabitation"

reghdfe ptoim1 treatPost treat if (mother_employed==1),  absorb($fe)  cluster($cluster)
est sto mum_empl1
reghdfe ptoim1 treatPost treat if (mother_employed==0),  absorb($fe)  cluster($cluster)
est sto mum_empl0

reghdfe ptoim1 treatPost treat if (mom_educ >= 6),  absorb($fe)  cluster($cluster)
est sto mum_ed1
reghdfe ptoim1 treatPost treat if (mom_educ < 6),  absorb($fe)  cluster($cluster)
est sto mum_ed0

reghdfe ptoim1 treatPost treat if (sp_mom_educ >= 6),  absorb($fe)  cluster($cluster)
est sto pmum_ed1
reghdfe ptoim1 treatPost treat if (sp_mom_educ < 6),  absorb($fe)  cluster($cluster)
est sto pmum_ed0

reghdfe ptoim1 treatPost treat if (sp_mother_employed==1),  absorb($fe)  cluster($cluster)
est sto pmum_empl1
reghdfe ptoim1 treatPost treat if (sp_mother_employed==0),  absorb($fe)  cluster($cluster)
est sto pmum_empl0


reghdfe ptoim1 treatPost treat if (parent_EducGap <0),  absorb($fe)  cluster($cluster)
est sto gapmum_ed1
reghdfe ptoim1 treatPost treat if (parent_EducGap >=0),  absorb($fe)  cluster($cluster)
est sto gapmum_ed0


reghdfe ptoim1 treatPost treat if (sp_parent_EducGap <0),  absorb($fe)  cluster($cluster)
est sto pgapmum_ed1
reghdfe ptoim1 treatPost treat if (sp_parent_EducGap >=0),  absorb($fe)  cluster($cluster)
est sto pgapmum_ed0

coefplot (mum_ed1, label("College") bcolor(blue)) ///
				(mum_ed0, label("No College") bcolor(red)) ///
				(gapmum_ed1, label("> Ed") bcolor(green)) ///
				(gapmum_ed0, label("=/< Ed") bcolor(orange))  ///
					(mum_empl1, label("Employed") bcolor(purple)) ///
				(mum_empl0, label("Not Employed") bcolor(pink)) , ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.12) ///
			keep(treatPost) name(ptoim1_earn, replace) ///
			vertical legend(rows(2)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.1(0.02)0) yline(0) ytitle("Employment")	
graph export "$result_folder\victim_match_het_mumedempl_ptoim1.pdf", replace

			
coefplot (pmum_ed1, label("College") bcolor(blue)) ///
				(pmum_ed0, label("No College") bcolor(red)) ///
				(pgapmum_ed1, label("> Ed") bcolor(green)) ///
				(pgapmum_ed0, label("=/< Ed") bcolor(orange))  ///
				(pmum_empl1, label("Employed") bcolor(purple)) ///
				(pmum_empl0, label("Not Employed") bcolor(pink)) , ///
			recast(bar) citop ciopts(recast(rcap)) barwidth(0.12) ///
			keep(treatPost) name(ptoim1_earn, replace) ///
			vertical legend(rows(2)) graphregion(color(white)) ///
			yscale(r(-.1 0)) ylabel(-0.1(0.02)0) yline(0) ytitle("Employment")	
graph export "$result_folder\victim_match_het_perpmumedempl_ptoim1.pdf", replace



